/**
  ******************************************************************************
  * @file    app_mqtt.c
  * @author  zwx
  * @version V1.0.0
  * @date    2019-09-21
  * @brief   
  ******************************************************************************
  */  

#include "app_mqtt.h"

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "heap_6.h"

#include "srv_esp8266.h"
#include "srv_mqtt.h"
#include "srv_cjson.h"

#include <stdio.h>

#define ESP_Printf printf

/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
enum WIFI_STATUS
{
	CHECK_ESP,
	CLOSE_ATE0,
	CONNECT_WIFI,
	CONNECT_SERVER,
	CONNECT_MQTT,
	DO_CJSON,
	SEND_DATA,
	DO_NOTHING,
};
/* Private struct  -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

volatile uint8_t Mqtt_DoState = CHECK_ESP;
static char *pub_data = NULL;
/* Private functions ---------------------------------------------------------*/


/**
  * @brief  
  * @param  None
  * @retval None
  */
void vTaskMqtt(void *pvParameters)
{
    while(1)
    {
		switch(Mqtt_DoState)
		{
			case CHECK_ESP:
				if(CheckEsp8266() == ESP_FIND_OK)
				{
					ESP_Printf("[ESP] Find ESP8266!\n");
					Mqtt_DoState = CLOSE_ATE0;					
				}
				else
				{
					ESP_Printf("[ESP] Can't Find ESP8266!\n");
					Mqtt_DoState = CHECK_ESP;
				}
				break;
			case CLOSE_ATE0:
				if(CloseATE() == ESP_CLOSEATE0_OK)
				{
					ESP_Printf("[ESP] Close ATE0 OK\n");
					Mqtt_DoState = CONNECT_WIFI;
				}
				else
				{
					ESP_Printf("[ESP] Close ATE0 Fail!\n");
					Mqtt_DoState = CLOSE_ATE0;
				}			
				break;				
			case CONNECT_WIFI:
				if(ConnectWiFi() == WIFI_CON_OK)
				{
					ESP_Printf("[ESP] Connect WIFI OK\n");
					Mqtt_DoState = CONNECT_SERVER;
				}
				else
				{
					ESP_Printf("[ESP] Connect WIFI Fail\n");
					Mqtt_DoState = CONNECT_WIFI;
				}				
				break;
			case CONNECT_SERVER:
				if(ConnectServer() == SERVER_CON_OK)
				{
					ESP_Printf("[ESP] Connect Server OK\n");
					Mqtt_DoState = CONNECT_MQTT;
				}
				else
				{
					ESP_Printf("[ESP] Connect Server Fail\n");
					Mqtt_DoState = CONNECT_SERVER;
				}	
				break;
			case CONNECT_MQTT:
				if(Mqtt_Connect() == MQTT_CON_OK)
				{
					ESP_Printf("[ESP] Connect MQTT Server OK\n");
					Mqtt_DoState = DO_CJSON;
				}
				else
				{
					ESP_Printf("[ESP] Connect MQTT Server Fail\n");
					Mqtt_DoState = CONNECT_MQTT;
				}							
				break;				
			case DO_CJSON:
				dataTocJSON(&pub_data);
				if(pub_data != NULL)
				{
//					printf(pub_data);
//					printf("\n");
					Mqtt_DoState = SEND_DATA;
				}
				else
				{
					Mqtt_DoState = DO_CJSON;
				}		
				break;
			case SEND_DATA:
				mqtt_publish(pub_data);
				vMy_Free(pub_data);	
			Mqtt_DoState = DO_CJSON;
			vTaskDelay(100);
				break;
			case DO_NOTHING:
				break;
			default:
				break;
		}

        vTaskDelay(5);
    }
}






/*****************************  End   ******************************************/



